<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\AuthController;
use Think\Auth;

class OrderController extends AuthController {
	
    public function audit(){

         $key=I('key');
        if($key&&$key!=""){    
            $map['u.truename']=array('like', "%".$key."%");
        }

        $sldate=urldecode(I('reservation',''));//获取格式 2015-11-12 - 2015-11-18
        $arr = explode(" - ",$sldate);//转换成数组
        $arrdateone=strtotime($arr[0]);
        $arrdatetwo=strtotime($arr[1].' 23:55:55'); 
        if($arrdateone && $arrdatetwo){
            $map['o.addtime'] = array(array('egt',$arrdateone),array('elt',$arrdatetwo),'AND');
        }  

    	$Model=M('order as o');
        $limits=C('PAGE_SIZE');
    	$list=$Model->join('JOIN lh_user as u ON o.user_id = u.id')->where($map)->page(I('get.p'),$limits)->field('o.id as orderid,o.addtime as addtime,o.audit_time as audit_time,o.order_num as order_num,o.desc as orderdesc,o.paystatus,u.mobile as mobile,u.truename as truename,u.idcard as idcard')->order('o.addtime DESC')->select();	

         foreach($list as $k=>$v)
        {          
            $list[$k]['addtime']=date('Y年m月d日 H:i:s',$v['addtime']);
        }
   
        $count = $Model->count(); 
         $p = getpage($count,$limits);
         //$p = getpage($count,C('PAGE_SIZE'));
         $show = $p->show(); 
        $allpage = ceil($count / $limits);
        $allpage = intval($allpage);
        $Nowpage = $_GET['page']?$_GET['page']:1;


        if($_GET['page']){
            $this->success($list);
        }
         $this->assign('page', $show); 
        $this->assign('Nowpage', $Nowpage);
        $this->assign('allpage', $allpage);
        $this->assign('s',$sldate);
        $this->assign('list', $list);
        $this->assign('val',$key);
		$this->display();
    }

    public function edit()
    {   
     $orderid=I('orderid');
        $res=$this->edit_after_addorder($orderid);
        if ($res) {
          $this->success('审核成功',U('audit'),1);
        }
    }


     public function add_order(){
       /* $id=6;
        $n=10;
          D('order')->getOrderNums_up($id,$n);
          exit();*/
       
        if (!IS_AJAX){
          
        $this->display();
        }else{

            $order=M('order');
            
            $data=$_POST;
             $moshi=M('moshi')->where('status=1')->find();
            $jifen=M('jifen')->find();
            $data['audit_time'] = strtotime(I('post.audit_time'));
            $data['order_code'] = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
            $data['addtime'] = time();
            $data['paystatus'] = 1;
            $data['invite_id'] = M('user')->where(array('id'=>I('post.user_id')))->getField('invite_id');
            $data['moshi_week1_num'] = $moshi['moshi_week1_num'];
            $data['moshi_week2_num'] = $moshi['moshi_week2_num'];
            $data['moshi_cf_week1'] = $moshi['moshi_cf_jifen1']*$data['order_num'];
            $data['moshi_xf_week1'] = $moshi['moshi_xf_jifen1']*$data['order_num'];
            $data['moshi_xf_week2'] = $moshi['moshi_xf_jifen2']*$data['order_num'];
            $data['moshi_cf_week2'] = $moshi['moshi_cf_jifen2']*$data['order_num'];
            $data['moshi_total_xf'] = $jifen['jifen_xf_one']*$data['order_num'];
            $data['xf_jifen'] = $jifen['jifen_xf_one'];
            $data['jifen_xf_ten'] = $jifen['jifen_xf_ten'];
            $map['user_id']=$userid;
         $map['xf_status']=0;
         $map['paystatus']=1;
                $orderxf_num=D('order')->getTotalNums_before($map);
                if ($orderxf_num>=10) {

                    $data['self_xf_remain']=$orderxf_num-10;
                }


             $self_xf_remain=M('order')->where(array('user_id'=>I('post.user_id')))->order('id desc')->getField('self_xf_remain');

             if ($self_xf_remain>0) {
                  $orderxf_num=I('post.order_num')+$self_xf_remain; 
                $data['self_xf_remain']= $orderxf_num>=10? $orderxf_num-10:0;
             
             }else{
         $map['user_id']=$userid;
         $map['xf_status']=0;
         $map['paystatus']=1;
                $orderxf_num=D('order')->getTotalNums_before($map);
                $data['self_xf_remain']= $orderxf_num>=10? $orderxf_num-10:0;
             }
            $a = date("w",$data['audit_time']);


if($a =="0"){
      $d=date("Y-m-d 00:00:00",$data['audit_time']);
}else{
    $d=date("Y-m-d 00:00:00",strtotime('next sunday',$data['audit_time']));
}
            $data['begin_hf_time'] =strtotime($d)+604800; 

            $weeknum=$moshi['moshi_week1_num']+$moshi['moshi_week2_num'];

            for ($i=1; $i <= $weeknum; $i++) { 
                $data['hf_endtime'] =strtotime($d)+604800*$i;
                $data['order_id'] =strtotime($d)+604800*$i;
                $data['hf_endtime'] =strtotime($d)+604800*$i;
            }

            $data['week1end_hf_time'] =strtotime($d)+604800*$moshi['moshi_week1_num']; 
            $data['end_hf_time'] =strtotime($d)+604800*$weeknum; 
            $result=$order->add($data);

           
               $this->after_addorder($result);
               
            $this->success('订单添加成功',U('audit'),1);
        }  
        
    }


    public function after_addorder($orderid)
    {  
        $jifen_xf_one= M('jifen')->getField('jifen_xf_one');
       $orderinfo= M('order')->find($orderid);
        $data['id']=$orderid;
        $data['paystatus']=1;
        $data['xf_jifen']=$jifen_xf_one;
        $userid=M('order')->where("id=$orderid")->getField('user_id');
       $invite_id=M('order')->where("id=$orderid")->getField('invite_id');
     
         $res1=M('order')->save($data);
         $add_xfjifen=$orderinfo['moshi_total_xf'];
         $res2=M('user')->where("id=$userid")->setInc("xf_jifen",$add_xfjifen);
         $res3=M('user')->where("id=$userid")->setInc("ordernums",$orderinfo['order_num']);
         $record['user_id']=$userid;
         $record['items']='下单奖励积分';
         $record['jifen_num']='+'.$add_xfjifen;
         $record['addtime']=time();
         $record['jifen_type']='xf';
         M('jifen_record')->add($record);
        $status=M('user')->where("id=$userid")->getField('status');

        $moshi=M('moshi')->where('status=1')->find();
        $weeknum=$orderinfo['moshi_week1_num']+$orderinfo['moshi_week2_num'];
                    $a = date("w",$orderinfo['audit_time']);
        if($a =="0"){
              $d=date("Y-m-d 00:00:00",$orderinfo['audit_time']);
        }else{
            $d=date("Y-m-d 00:00:00",strtotime('next sunday',$orderinfo['audit_time']));
        }
        $curtime=time();
           for ($i=1; $i <= $orderinfo['moshi_week1_num']; $i++) { 
                //unset($data);
                $data2['order_id'] =$orderid;
                $data2['orderinfo'] = $orderinfo['orderinfo'];
                $data2['hf_endtime'] =strtotime($d)+604800*$i; 
                $data2['cf_jifen'] =$orderinfo['moshi_cf_week1'];
                $data2['xf_jifen'] =$orderinfo['moshi_xf_week1'];
                if ($data2['hf_endtime']<=$curtime) {
                  $data2['status']=1;
                 M('user')->where("id=$userid")->setInc("cf_jifen",$orderinfo['moshi_cf_week1']);
             $record['user_id']=$userid;
             $record['items']='每周返还财富积分';
             $record['jifen_num']='+'.$orderinfo['moshi_cf_week1'];
             $record['addtime']=$data2['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);

                M('order')->where("id=".$orderid)->setInc('moshi_week1_fh_num');
                }else{
                  $data2['status']=0;
                }
               
                M('order_fh')->add($data2);
            }

             for ($i=$orderinfo['moshi_week1_num']+1; $i <= $weeknum; $i++) { 
                //unset($data);
                $data3['order_id'] =$orderid;
                $data3['orderinfo'] = $orderinfo['orderinfo'];
                $data3['hf_endtime'] =strtotime($d)+604800*$i;
                $data3['xf_jifen'] =$orderinfo['moshi_xf_week2'];
                $data3['cf_jifen'] =$orderinfo['moshi_cf_week2'];
                if ($data3['hf_endtime']<=$curtime) {
                  unset($record);
                  $data3['status']=1;
                  M('user')->where("id=$userid")->setInc("cf_jifen",$orderinfo['moshi_cf_week2']);
                  M('user')->where("id=$userid")->setInc("xf_jifen",$orderinfo['moshi_xf_week2']);

             $record['user_id']=$userid;
             $record['items']='每周返还财富积分';
             $record['jifen_num']='+'.$orderinfo['moshi_cf_week2'];
             $record['addtime']=$data3['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);

             $record2['user_id']=$userid;
             $record2['items']='每周返还消费积分';
             $record2['jifen_num']='+'.$orderinfo['moshi_xf_week2'];
             $record2['addtime']=$data3['hf_endtime'];
             $record2['jifen_type']='xf';
             M('jifen_record')->add($record2);

              M('order')->where("id=".$orderid)->setInc('moshi_week2_fh_num');
                }else{
                  $data3['status']=0;
                }

                M('order_fh')->add($data3);
            }
    

         $map['user_id']=$userid;
      
         $map['paystatus']=1;
         $orderxf_num=D('order')->getTotalNums($map);

          //自身消费满10单，送上级积分 
        if ($orderxf_num>=10) {
            //总分的工资订单数
         $yfh_gz=floor($orderxf_num/10);
         $c['user_id']=$invite_id;
         $c['items']='您邀请的好友超10单奖励积分';
         $c['jifen_type']='xf';
  
            $fh_jf=M('jifen_record')->where($c)->count();

            $n=$yfh_gz-$fh_jf;

          for ($i=1; $i <=$n ; $i++) { 
         $record['user_id']=$invite_id;
         $record['items']='您邀请的好友超10单奖励积分';
         $record['jifen_num']='+'.$orderinfo['jifen_xf_ten'];
         $record['addtime']=time();
         $record['jifen_type']='xf';
         M('jifen_record')->add($record);
          M('user')->where("id=$invite_id")->setInc("xf_jifen",$orderinfo['jifen_xf_ten']); 
          }
        
            $f_id=M('user')->where("id=$userid")->getField('f_id');
       if ($status>1) {
        unset($map);

        if ($f_id!=0) {

              $yfh_gz=floor($orderxf_num/10);
            $map['user_id']=$userid;
            $gz['user_id']=$userid;
            $gz['desc']='自身消费满10单';

            $fh_gz=M('order_gz')->where($gz)->count();
            $n=$yfh_gz-$fh_gz;
           
            for ($i=1; $i <=$n ; $i++) { 

                  $curtime=time();
            //D('order')->where($map)->setField('self_gz_status',1);
               unset($data);
               $jifen=M('jifen')->find();
                $data['user_id']=$userid;
                $data['addtime']=$orderinfo['audit_time'];
                //$data['addtime']=$curtime;
                $data['fh_cfjf']=$jifen['jifen_cf_gz'];
                $data['desc']='自身消费满10单';
                $data['fh_weeks']=$moshi['moshi_gz_weeks'];
                    $a = date("w",$data['addtime']);
            if($a =="0"){
                  $d=date("Y-m-d 00:00:00",$data['addtime']);
            }else{
                $d=date("Y-m-d 00:00:00",strtotime('next sunday',$data['addtime']));
            }
            $data['begin_hf_time'] =strtotime($d)+604800; 

                $addgz=M('order_gz')->add($data);
                  $gz=M('order_gz')->find($addgz);
             
                 for ($i=1; $i <= $gz['fh_weeks']; $i++) { 
                $data_fh2['gz_id'] =$addgz;
                $data_fh2['hf_endtime'] =strtotime($d)+604800*$i;
                $data_fh2['cf_jifen'] =$gz['fh_cfjf'];
                if ($data_fh2['hf_endtime']<=$curtime) {
                  $data_fh2['status']=1;
                 M('user')->where("id=$userid")->setInc("cf_jifen",$gz['fh_cfjf']);
             $record['user_id']=$userid;
             $record['items']='工资订单每周返还财富积分';
             $record['jifen_num']='+'.$gz['fh_cfjf'];
             $record['addtime']=$data_fh2['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);
                M('order_gz')->where("id=".$addgz)->setInc('already_fh_weeks');
                }else{
                  $data_fh2['status']=0;
                }
               
                M('order_gz_hf')->add($data_fh2);
            }




            }

        }

              
    }

 }

           $jb=M('jbset')->find();
switch ($status) {
              case '1':
                  $months=$jb['sj_jb2_mons']-1;
                  break;
            case '2':
                  $months=$jb['sj_jb3_mons']-1;
                  break;
            case '3':
                   $months=$jb['sj_jb4_mons']-1;
                  break;
              
              default:
                  # code...
                  break;
          }

          $end=time();
         $begin=mktime(0,0,0,date('m')-$months,1,date('Y')); 
         D('Order')->getOrderNums_fl($userid,$orderinfo['order_num']);
            //迭代工资订单
         D('Order')->getOrderNums_up($userid,$orderinfo['order_num']);
         //迭代升级
    $getOrderNums_team =D('Order')->getTeamTotalNumsUpCum($userid,$begin,$end);

        //获取上级id 自身团队
        $invite_id=M('user')->where("id=$userid")->getField('invite_id');
        if ($invite_id>0) {
             //是否达到自动解绑
            $invite_status=M('user')->where("id=$invite_id")->getField('status');
             if ($status>=$invite_status && $status>1 ) {
                 
                         
                         $beyond_months= M('jifen')->getField('beyond_months');
                          $beyond['beyond_end_time']= strtotime("+".$beyond_months."months");
                          $beyond['beyond_sj_id']=$invite_id;
                          $beyond['beyond_time']=time();
                      M('user')->where("id=$userid")->save($beyond);
        }   
           

    }
   

         if ($res2 && $res1 && $res3) {
            M()->commit(); 
           return true;
         }else{
           M()->rollback();
           return false;
         }

    }


   

     public function edit_after_addorder($orderid)
    {
      
        $jifen_xf_one= M('jifen')->getField('jifen_xf_one');
        $orderinfo= M('order')->find($orderid);
        $data['audit_time']=time();
        $data['id']=$orderid;
        $data['paystatus']=1;
        
        $res1=M('order')->save($data);
        $userid=M('order')->where("id=$orderid")->getField('user_id');
        $invite_id=M('user')->where("id=$userid")->getField('invite_id');
        $f_id=M('user')->where("id=$userid")->getField('f_id');
     
         
         $add_xfjifen=$orderinfo['moshi_total_xf'];
         $res2=M('user')->where("id=$userid")->setInc("xf_jifen",$add_xfjifen);
         $res3=M('user')->where("id=$userid")->setInc("ordernums",$orderinfo['order_num']);
         $record['user_id']=$userid;
         $record['items']='下单奖励积分';
         $record['jifen_num']='+'.$add_xfjifen;
         $record['addtime']=time();
         $record['jifen_type']='xf';
         M('jifen_record')->add($record);
        $status=M('user')->where("id=$userid")->getField('status');

        $moshi=M('moshi')->where('status=1')->find();
          $weeknum=$orderinfo['moshi_week1_num']+$orderinfo['moshi_week2_num'];
                    $a = date("w",$data['audit_time']);
            if($a =="0"){
                  $d=date("Y-m-d 00:00:00",$data['audit_time']);
            }else{
                $d=date("Y-m-d 00:00:00",strtotime('next sunday',$data['audit_time']));
            }
            $curtime=time();
           for ($i=1; $i <= $orderinfo['moshi_week1_num']; $i++) { 
                //unset($data);
                $data2['order_id'] =$orderid;
                $data2['orderinfo'] = $orderinfo['orderinfo'];
                $data2['hf_endtime'] =strtotime($d)+604800*$i; 
                $data2['cf_jifen'] =$orderinfo['moshi_cf_week1'];
                $data2['xf_jifen'] =$orderinfo['moshi_xf_week1'];
                if ($data2['hf_endtime']<=$curtime) {
                  $data2['status']=1;
                 M('user')->where("id=$userid")->setInc("cf_jifen",$orderinfo['moshi_cf_week1']);
             $record['user_id']=$userid;
             $record['items']='每周返还财富积分';
             $record['jifen_num']='+'.$orderinfo['moshi_cf_week1'];
             $record['addtime']=$data2['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);

                M('order')->where("id=".$orderid)->setInc('moshi_week1_fh_num');
                }else{
                  $data2['status']=0;
                }
               
                M('order_fh')->add($data2);
            }

             for ($i=$orderinfo['moshi_week1_num']+1; $i <= $weeknum; $i++) { 
                //unset($data);
                $data3['order_id'] =$orderid;
                $data3['orderinfo'] = $orderinfo['orderinfo'];
                $data3['hf_endtime'] =strtotime($d)+604800*$i;
                $data3['xf_jifen'] =$orderinfo['moshi_xf_week2'];
                $data3['cf_jifen'] =$orderinfo['moshi_cf_week2'];
                if ($data3['hf_endtime']<=$curtime) {
                  unset($record);
                  $data3['status']=1;
                  M('user')->where("id=$userid")->setInc("cf_jifen",$orderinfo['moshi_cf_week2']);
                  M('user')->where("id=$userid")->setInc("xf_jifen",$orderinfo['moshi_xf_week2']);

             $record['user_id']=$userid;
             $record['items']='每周返还财富积分';
             $record['jifen_num']='+'.$orderinfo['moshi_cf_week2'];
             $record['addtime']=$data3['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);

             $record2['user_id']=$userid;
             $record2['items']='每周返还消费积分';
             $record2['jifen_num']='+'.$orderinfo['moshi_xf_week2'];
             $record2['addtime']=$data3['hf_endtime'];
             $record2['jifen_type']='xf';
             M('jifen_record')->add($record2);

              M('order')->where("id=".$orderid)->setInc('moshi_week2_fh_num');
                }else{
                  $data3['status']=0;
                }

                M('order_fh')->add($data3);
            }
    

         $map['user_id']=$userid;
      
         $map['paystatus']=1;
         $orderxf_num=D('order')->getTotalNums($map);
       

          //自身消费满10单，送上级积分 
        if ($orderxf_num>=10) {
            //总分的工资订单数
         $yfh_gz=floor($orderxf_num/10);
         $c['user_id']=$invite_id;
         $c['items']='您邀请的好友超10单奖励积分';
         $c['jifen_type']='xf';
  
        $fh_jf=M('jifen_record')->where($c)->count();
            $n=$yfh_gz-$fh_jf;

          for ($i=1; $i <=$n ; $i++) { 
         $record['user_id']=$invite_id;
         $record['items']='您邀请的好友超10单奖励积分';
         $record['jifen_num']='+'.$orderinfo['jifen_xf_ten'];
         $record['addtime']=time();
         $record['jifen_type']='xf';
         M('jifen_record')->add($record);
          M('user')->where("id=$invite_id")->setInc("xf_jifen",$orderinfo['jifen_xf_ten']); 
          }

          //消费者以上自身消费满10单,产生工资订单
          if ($status>1) {
            if ($f_id==0) {
                
                D('order')->self10_half_getOrderNums_up($userid,$orderxf_num,'自身消费满10单',2);
                D('order')->self10_half_getOrderNums_up($invite_id,$orderxf_num,'自身消费满10单',2);
            }
            if ($f_id!=0) {
               
                D('order')->self10_half_getOrderNums_up($userid,$orderxf_num,'自身消费满10单',1);
            }
              
    }
         
        }

         

   $jb=M('jbset')->find();

       switch ($status) {
              case '1':
                  $months=$jb['sj_jb2_mons']-1;
                  break;
            case '2':
                  $months=$jb['sj_jb3_mons']-1;
                  break;
            case '3':
                   $months=$jb['sj_jb4_mons']-1;
                  break;
              
              default:
                  # code...
                  break;
          }

        $end=time();
         $begin=mktime(0,0,0,date('m')-$months,1,date('Y'));
D('Order')->getOrderNums_fl($userid,$orderinfo['order_num']);
 $iteration =D('Order')->getOrderNums_up($userid,$orderinfo['order_num']); 
$getOrderNums_team = D('Order')->getTeamTotalNumsUpCum($userid,$begin,$end);

        //获取上级id 自身团队
        $invite_id=M('user')->where("id=$userid")->getField('invite_id');
        if ($invite_id) {
             //是否达到自动解绑
            $invite_status=M('user')->where("id=$invite_id")->getField('status');
             if ($status>=$invite_status && $status>1 ) {
               
                  $beyond_months= M('jifen')->getField('beyond_months');
                          $beyond['beyond_end_time']= strtotime("+".$beyond_months."months");
                          $beyond['beyond_sj_id']=$invite_id;
                          $beyond['beyond_time']=time();
                      M('user')->where("id=$userid")->save($beyond);
            }

     
     

        }
       
     

        //return true;

         if ($res2 && $res1 && $res3) {
            M()->commit(); 
           return true;
         }else{
           M()->rollback();
           return false;
         }

    }

   


    public function delorder()
    {
        $id=I('id');
       $res=M('order')->delete($id);
       if ($res) {
          exit('1');
       }else{
        exit('-1');
       }

    }



}